//
//  SMyViewController.m
//  News
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SMyViewController.h"

#import "SShoucangViewController.h"
#import "SMyTableViewCell.h"
#import "SMyFuTableViewCell.h"
#import "SFankuiViewController.h"
#import "SDianyingBaseManager.h"
#import "SDataBaseManager.h"
#import "SDImageCache.h"
#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"
#import <ShareSDK/ShareSDK.h>

static CGFloat tupianHeight = 250;
static CGFloat tupianTempHeight = 60.0f;
@interface SMyViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>


@property(nonatomic, retain) UIAlertView *al;

@property(nonatomic ,assign)NSInteger i;
@property(nonatomic, assign)BOOL a;
@property(nonatomic ,retain)UIButton *dianji;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UIImageView *MyView;
@property(nonatomic, retain)NSMutableArray *ImageArr;

@property(nonatomic, retain) UIAlertView *alertView;

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *touxiang;

@property (nonatomic, retain) UIImageView *My;
@property (nonatomic, retain) UILabel *MyLabel;
@property (nonatomic, assign) BOOL b;


@end

@implementation SMyViewController



- (void)dealloc
{    [_al release];
    [_ImageArr release];
    [_alertView release];
    [_dianji release];
    [_arr release];
    [_tableView release];
    [_MyView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
    self.a = NO;
    self.arr = [@[@"夜间模式", @"清除缓存", @"我的收藏", @"关于我们", @"反馈信息"]mutableCopy];
    self.ImageArr = [@[@"1", @"2", @"3", @"4", @"5"]mutableCopy];
    [self jianli];
    //建立view放置登录按钮
   self.MyView = [[UIImageView alloc] initWithFrame:CGRectMake(0, - tupianHeight - tupianTempHeight, self.tableView.frame.size.width, tupianHeight + tupianTempHeight)];

   _MyView.image = [UIImage imageNamed:@"{(BINJI2WJJ2~E7BG`D18{G"];
    [self.tableView addSubview:_MyView];
    
    self.dianji = [UIButton buttonWithType:UIButtonTypeSystem];
    _dianji.frame = CGRectMake((_MyView.frame.size.width - 80)/ 2, _MyView.frame.origin.y / 2 - 50, 80, 80);
    _dianji.layer.cornerRadius = 40.0;//（该值到一定的程度，就为圆形了。）
    
    _dianji.layer.borderWidth = 1.0;
    
    _dianji.layer.borderColor =[UIColor clearColor].CGColor;
    
    _dianji.clipsToBounds = TRUE;//去除边界
    self.My = [[[UIImageView alloc] initWithFrame:_dianji.bounds] autorelease];
    _My.image = [UIImage imageNamed:@"VPR]J$5VPYYBWJN80}[Z)6X"];
    self.MyLabel = [[UILabel alloc] initWithFrame:CGRectMake((_MyView.frame.size.width - 140)/ 2, _MyView.frame.origin.y / 2 + 30, 140, 30)];
    _MyLabel.textAlignment = NSTextAlignmentCenter;
    _MyLabel.textColor = [UIColor redColor];
    _MyLabel.text = @"点击登录";
    [_dianji addSubview:_My];
    [self.tableView addSubview:_MyLabel];
    [self.tableView addSubview:_dianji];
    [_dianji addTarget:self action:@selector(denglu) forControlEvents:UIControlEventTouchUpInside];
    
    self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您确定清除" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [self.view addSubview:_alertView];
    
    
}

- (void)zhuxiao
{
    
    [self zhuangtai];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(zhuxiao)];
    if (_b == 1) {
        [ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
        _MyLabel.text = @"点击登录";
        _My.image = [UIImage imageNamed:@"VPR]J$5VPYYBWJN80}[Z)6X"];
        _b = NO;
    }
}

#pragma 清理缓存图片

- (void)clearTmpPics
{

    NSInteger size = [[SDImageCache sharedImageCache] getSize];
    float flSize = size /1024.0 /1024.0;

    
     NSString * str = [NSString stringWithFormat:@"%.2f.M",flSize];
    
  
    
    self.alertView = [[UIAlertView alloc] initWithTitle:@"本地缓存" message:str delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"确定", nil];
   
   [_alertView show];
    
    [_alertView release];
}




//建立tableView;
- (void)jianli
{
    self.tableView= [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(tupianHeight, 0, 0, 0);
    [_tableView release];
}
-  (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yOffset  = scrollView.contentOffset.y;
    CGFloat xOffset = (yOffset + tupianHeight)/2;
    if (yOffset < - tupianHeight) {
        CGRect f = self.MyView.frame;
        f.origin.y = yOffset - tupianTempHeight;
        f.size.height =  -yOffset + tupianTempHeight;
        f.origin.x = xOffset;
        f.size.width = self.view.frame.size.width + fabsf(xOffset)*2;
        self.MyView.frame = f;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    SMyFuTableViewCell *fu = [tableView dequeueReusableCellWithIdentifier:@"fu"];
    if (indexPath.row == 0) {
        if (fu == nil) {
            fu = [[[SMyFuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"fu"] autorelease];
        }
        fu.MyLabel.text = _arr[indexPath.row];
        fu.MyImage.image= [UIImage imageNamed:_ImageArr[indexPath.row]];
        fu.sw.on = self.a;
        [fu.sw addTarget:self action:@selector(suibian) forControlEvents:UIControlEventValueChanged];
        fu.sw.tag = 1000;
        return fu;
    }
    
    else {
        SMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"str"];
    
    if (cell == nil) {
        cell = [[SMyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"str"];
    }
        cell.MyLabel.text = _arr[indexPath.row];
       cell.MyImage.image= [UIImage imageNamed:_ImageArr[indexPath.row]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    
    
}

- (void)suibian
{
    
    UISwitch *p = [[[UISwitch alloc] init] autorelease];
    p = (UISwitch *)[self.view viewWithTag:1000];
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    
    if (p.on == YES) {
        [center postNotificationName:@"yejianmoshi" object:@"日间"];
    }else if (p.on == NO){
        [center postNotificationName:@"yejianmoshi" object:@"夜间"];
    }
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([_arr [indexPath.row] isEqualToString:@"夜间模式"]) {
        
    }
    if ([_arr[indexPath.row] isEqualToString:@"清除缓存"]) {
        
        [self clearTmpPics];
        
    }
    if ([_arr [indexPath.row] isEqualToString:@"我的收藏"]) {
        SShoucangViewController *shoucang = [[[SShoucangViewController alloc] init] autorelease];
        [self.navigationController pushViewController:shoucang animated:YES];

    }
    if ([_arr [indexPath.row] isEqualToString:@"关于我们"]) {
        
        self.al = [[UIAlertView alloc] initWithTitle:@"PEAK团队" message:@"L C Z\n欢迎您的使用\n当前版本1.0.1" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [_al show];
        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(quxiao) userInfo:nil repeats:NO];
    }
    if ([_arr [indexPath.row] isEqualToString:@"反馈信息"]) {
        SFankuiViewController *fankui = [[[SFankuiViewController alloc] init] autorelease];
        [self.navigationController pushViewController:fankui animated:YES];
        
           }
}
   //alerView点击方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex == 1) {
        //清除磁盘缓存
        [[SDImageCache sharedImageCache] clearDisk];
        //清除内存缓存
        [[[SDWebImageManager sharedManager] imageCache] clearMemory];
        
        //清除系统缓存
        [[NSURLCache sharedURLCache] removeAllCachedResponses];

    }
}

//按钮点击方法
- (void)denglu
{

    ShareType type = 0;
    
    type = ShareTypeSinaWeibo;
    
    [ShareSDK getUserInfoWithType:type authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
        if (result) {
           // NSLog(@"授权登陆成功，已获取用户信息");
           // NSString *uid = [userInfo uid];
            NSString *nickname = [userInfo nickname];
            NSString *profileImage = [userInfo profileImage];
            self.name = nickname;
            self.touxiang = profileImage;
            self.MyLabel.text = self.name;
             [_My sd_setImageWithURL:[NSURL URLWithString:self.touxiang]];
            NSLog(@"===%@", self.touxiang);
        }else{
            NSLog(@"分享失败,错误码:%ld,错误描述%@",(long)[error errorCode],[error errorDescription]);
            
        }
        [self zhuangtai];
    }];


}

- (void)zhuangtai
{
   self.b =  [ShareSDK hasAuthorizedWithType:ShareTypeSinaWeibo];
    if (_b == 1) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(zhuxiao)];
    }
    
}


- (void)quxiao
{
    [_al dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
